package cn.tlb.ui.custom.progressbutton;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Random;

import cn.tlb.xutilsdownload.R;

public class NewUiTestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_ui_test);

        pb = (ProgressButton) findViewById(R.id.progressButton);
        pb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(NewUiTestActivity.this, "ProgressButton", Toast.LENGTH_SHORT).show();
                if(pb.isDownloading()){
                    handler.removeCallbacks(mRunnable);
                    pb.defaultStyle();
                }else{
                    pb.downloadingStyle();
                    handler.post(mRunnable);
                }
            }
        });

        ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(new MyAdapter());
    }

    ProgressButton pb;

    Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            pb.setProgress(pb.getProgress() + 5);
            if(pb.getProgress() >= pb.getMax()){
                pb.installStyle();
                handler.removeCallbacks(this);
            }else {
                handler.postDelayed(this, 300);
            }
        }
    };

    Handler handler = new Handler()/*{
        @Override
        public void handleMessage(Message msg) {
            pb.setProgress(msg.what);
            if(msg.what >= pb.getMax()) {
                pb.installStyle();
                removeCallbacks(mRunnable);
            }
        }
    }*/;

    private class MyAdapter extends BaseAdapter {
        ArrayList<String> strings;
        {
            strings = new ArrayList<>();
            for (int i = 0; i < 50; i++) {
                strings.add("item: "+i);
            }
        }
        @Override
        public int getCount() {
            return strings.size();
        }

        @Override
        public String getItem(int position) {
            return strings.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if(convertView == null){
                convertView = getLayoutInflater().inflate(R.layout.list_item, null);
            }

            ((TextView)convertView.findViewById(R.id.text)).setText(getItem(position));
            final ProgressButton progressButton = (ProgressButton) convertView.findViewById(R.id.ProgressButton);

            final Handler handler = new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    progressButton.setProgress(msg.what);
                    if(msg.what < progressButton.getMax()) {
                        progressButton.setText((CharSequence) msg.obj);
                    }else{
                        progressButton.setText("下载完成");
                    }
                }
            };
            progressButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new Thread() {
                        @Override
                        public void run() {
                            progressButton.setProgress(0);
                            while (progressButton.getProgress() <= progressButton.getMax()) {
                                try {
                                    Thread.sleep(new Random().nextInt(300));
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                Message.obtain(handler, progressButton.getProgress() + 5, "下载中").sendToTarget();
                            }
                        }
                    }.start();
                }
            });


            return convertView;
        }
    }
}
